package com.hengxinguotong.zhihuichengjian.ui.building;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceListPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXEditTextView;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/2/18
 */

public class ReplyListEnterpriseActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.et_project)
    HXEditTextView etProject;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private List<BaseFragment> fragmentList;
    private DeviceListPagerAdapter deviceListPagerAdapter;
    private List<String> mTitleDataList;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reply_list_enterprise);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", Constants.LIVE);
        initView();
    }

    private void initView() {
        viewPager.setOffscreenPageLimit(4);
        fragmentList = new ArrayList<BaseFragment>();
        ReplyListEnterpriseFragment daihuifuFragment = ReplyListEnterpriseFragment.newInstance(type, "1");
        daihuifuFragment.setActivity(this);
        fragmentList.add(daihuifuFragment);
        ReplyListEnterpriseFragment daifujianFragment = ReplyListEnterpriseFragment.newInstance(type, "2");
        daifujianFragment.setActivity(this);
        fragmentList.add(daifujianFragment);
        ReplyListEnterpriseFragment butongguoFragment = ReplyListEnterpriseFragment.newInstance(type, "3");
        butongguoFragment.setActivity(this);
        fragmentList.add(butongguoFragment);
        ReplyListEnterpriseFragment yiwanchengFragment = ReplyListEnterpriseFragment.newInstance(type, "4");
        yiwanchengFragment.setActivity(this);
        fragmentList.add(yiwanchengFragment);
        deviceListPagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(deviceListPagerAdapter);


        mTitleDataList = new ArrayList<String>();
        mTitleDataList.add("待回复");
        mTitleDataList.add("待复检");
        mTitleDataList.add("复检不通过");
        mTitleDataList.add("已完成");

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTitleDataList.get(index));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_666666));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_f288ae5));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.color_f288ae5));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return Utils.dip2px(ReplyListEnterpriseActivity.this, 20);
            }
        });

        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(2.0f));
        fragmentContainerHelper.setDuration(300);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
            }
        });

        etProject.addTextChangedListener(textWatcher);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            for (BaseFragment fragment:fragmentList){
                ((ReplyListEnterpriseFragment)fragment).onListFragmentInteraction(s.toString());
            }
        }
    };

    @OnClick({R.id.iv_back})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data != null){
            if(requestCode == 1 && resultCode == 1){
                User user = (User) data.getSerializableExtra("selectUser");
                String createBy = user.getUserId();
                etProject.setText(user.getName());
//                ReplyListEnterpriseFragment fragment = (ReplyListEnterpriseFragment) fragmentList.get(currentItem);

            }
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnListFragmentInteractionListener {
        // TODO: Update argument type and name
        void onListFragmentInteraction(String constructionName);
    }
}
